### Replication Package for "Why is Intermediating Houses so Difficult? Evidence from iBuyers"
### Buchak, Matvos, Piskorski, and Seru
###
###
### buchak@stanford.edu

### Creates iBuyer pricing behavior data

library(data.table)
library(ggplot2)
library(lfe)
library(Hmisc)
library(zoo)
library(stargazer)
library(scales)


source('0_helper_functions.r')

setDTthreads(20)

Table_2_Panel_B <- function() {
    data <- loadData()
    data <- data[year %in% 2013:2018]
    
    # Exclude sales that are far in excess of local prices (likely data error)
    prices <- data[saleamount > quantile(data$saleamount,.01,na.rm=T) & saleamount < quantile(data$saleamount,.99,na.rm=T),j=list(index.price = median(saleamount,na.rm=T)),by=c('zip5','qtr')]
    completed <- merge(data,prices,by=c('zip5','qtr'))
    
    # Basic specification is log(saleAmount ~ iBuyer + x'Gamma + FE)
    b3 <- felm(log(saleamount) ~ iBuyer.buyer   + log(living_sqft) + house.age  | paste(zip5,qtr)  +  age.bin + size.bin + multistory + airconditioning + quality + heating + garage + locationinfluence,data=completed[abs(saleamount / index.price - 1) < 1])
    s3 <- felm(log(saleamount) ~ iBuyer.seller  + log(living_sqft) + house.age  | paste(zip5,qtr)  +  age.bin + size.bin + multistory + airconditioning + quality + heating + garage + locationinfluence,data=completed[abs(saleamount / index.price - 1) < 1])
    s3b <- felm(log(saleamount) ~ iBuyer.seller + log(living_sqft) + house.age  | paste(zip5,year) +  age.bin + size.bin + multistory + airconditioning + quality + heating + garage + locationinfluence,data=completed[abs(saleamount / index.price - 1) < 1])
    b3b <- felm(log(saleamount) ~ iBuyer.buyer  + log(living_sqft) + house.age  | paste(zip5,year) +  age.bin + size.bin + multistory + airconditioning + quality + heating + garage + locationinfluence,data=completed[abs(saleamount / index.price - 1) < 1])
    
    
    # For 2018 only.
    completed <- completed[year == 2018]
    
    b3.2018 <- felm(log(saleamount) ~ iBuyer.buyer  + log(living_sqft) + house.age  | paste(zip5,qtr) +  age.bin + size.bin + multistory + airconditioning + quality + heating + garage + locationinfluence,data=completed[abs(saleamount / index.price - 1) < 1])
    s3.2018 <- felm(log(saleamount) ~ iBuyer.seller + log(living_sqft) + house.age  | paste(zip5,qtr) +  age.bin + size.bin + multistory + airconditioning + quality + heating + garage + locationinfluence,data=completed[abs(saleamount / index.price - 1) < 1])
    
    
 
    stargazer(b3,b3b,b3.2018,s3,s3b,s3.2018,type='html',
              keep = c('iBuyer.buyer','iBuyer.seller'),
              add.lines = list(c('Sample','2013-2018','2013-2018','2018','2013-2018','2013-2018','2018'),
                               c('Hedonics','Y','Y','Y','Y','Y','Y'),
                               c('Zip-Quarter FE','Y','N','Y','Y','N','Y'),
                               c('Zip-year FE','N','Y','N','N','Y','N')),
              omit.stat = c('adj.rsq','f','ser'),
              out='../out/tables/2B.html')

}


Table_2_Panel_B()



